package animal;
// 定义一般类 Animal
abstract class Animal {
    String name;

    // 构造方法
    public Animal(String name) {
        this.name = name;
    }

    // 抽象方法 makeSound，强制子类实现
    public abstract void makeSound();
}

// 派生类 Tiger
class Tiger extends Animal {
    public Tiger(String name) {
        super(name);
    }

    @Override
    public void makeSound() {
        System.out.println(name + " says: Rua!");
    }
}

// 派生类 Elephant
class Elephant extends Animal {
    public Elephant(String name) {
        super(name);
    }

    @Override
    public void makeSound() {
        System.out.println(name + " says: AHHH");
    }
}

// 派生类 Fox
class Fox extends Animal {
    public Fox(String name) {
        super(name);
    }

    @Override
    public void makeSound() {
        System.out.println(name + " says: jiji");
    }
}

// 派生类 Panda
class Panda extends Animal {
    public Panda(String name) {
        super(name);
    }

    @Override
    public void makeSound() {
        System.out.println(name + " says: Ho");
    }
}

// 主类
public class dw {
    public static void main(String[] args) {
        // 创建动物数组
        Animal[] animals = new Animal[4];

        // 随机添加动物对象
        animals[0] = new Tiger("Tiger");
        animals[1] = new Elephant("Elephant");
        animals[2] = new Fox("Fox");
        animals[3] = new Panda("Panda");

        // 循环调用每个动物的 makeSound 方法
        for (Animal animal : animals) {
            animal.makeSound();  // 多态性: 根据实际对象类型调用相应的 makeSound 方法
        }
    }
}
